package cn.kgc.jinxin.clothesstore.dao.impl;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

import cn.kgc.jinxin.clothesstore.dao.BaseDao;
import cn.kgc.jinxin.clothesstore.dao.OrderDao;
import cn.kgc.jinxin.clothesstore.entity.Order;

public class OrderDaoImpl implements OrderDao {

	@Override
	public void insert(Order order,Connection connection) throws Exception {
		PreparedStatement psm = null;
		ResultSet resultSet=null;
		try {
			String sql = "insert into `order`()" + "values(?,?,?,?)";
			psm = connection.prepareStatement(sql);
			psm.setString(1, order.getUser().getUserName());
			psm.setString(2, order.getReceiver());
			psm.setString(3, order.getReceiverPhone());
			psm.setString(4, order.getReceiverAddress());
			psm.executeUpdate();
			sql = "select @@identity";
			psm = connection.prepareStatement(sql);
			resultSet = psm.executeQuery();
			if (resultSet.next()) {
				order.setOrderId(resultSet.getLong(1));
			} 
		} finally {
			BaseDao.close(null, psm, resultSet);
		}

	}

	@Override
	public void update(Long orderId, Double orderTotalPrice, Connection connection) throws Exception {
		PreparedStatement psm = null;
		try {
			String sql = "update `order` set orderTotalprice=? where orderId = ?";
			psm = connection.prepareStatement(sql);
			psm.setDouble(1, orderTotalPrice);
			psm.setLong(2, orderId);
			psm.executeUpdate();
		} finally {
			BaseDao.close(null, psm, null);
		}	
		
	}

	@Override
	public Map<String, Double> selectTotalPriceByUser() throws Exception {
		Map<String,Double> map = new HashMap<String,Double>();
		Connection connection = null;
		PreparedStatement psm = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "";
			psm = connection.prepareStatement(sql);
			resultSet = psm.executeQuery();
			while (resultSet.next()) {
				map.put(resultSet.getString(1), resultSet.getDouble(2));
			} 
		} finally {
			BaseDao.close(connection, psm, resultSet);
		}
		return map;
	}

}






